package com.testng.oa.testcase;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.testng.oa.pub.oaindex;
import com.testng.oa.pub.oalogin;

import org.openqa.selenium.interactions.Actions;
public class oaindextest {
	private static WebDriver driver = null;
	private String baseUrl;
	private String handle;
	private Document doc;

	@BeforeClass
	public void setUp() throws Exception {
		
		driver = new ChromeDriver();
		baseUrl = "http://localhost/oa/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.get(baseUrl);
		driver.manage().window().maximize();
		handle = driver.getWindowHandle();
		oalogin.oa_login(driver, "admin", "123456");
		
		File xmlFile = new File("F:\\eclipse-workspace\\rockoa-java2selenum\\src\\com\\testng\\oa\\testdata\\index.xml");
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		doc = builder.parse(xmlFile);
	}
	
	@AfterClass
	public void tearDown() throws Exception {
		driver.quit();
	}
	
	@Test(enabled=true)
	public void testlogout() {
		
		oaindex.getlink(driver, "退出").click();
		assertTrue(oalogin.login_UserName(driver).isEnabled());
		oalogin.oa_login(driver, "admin", "123456");
	}
	
	@Test(enabled=true)
	public void testchangestyle() throws InterruptedException {
		oaindex.getlink(driver, "样式v").click();
		String inner = oaindex.getjingdianstyleok(driver).getAttribute("style");
		assertEquals(inner, oaindex.okstyle);
		oaindex.getlansestylemenu(driver).click();
		Thread.sleep(6000);
		inner = oaindex.getlansestyleok(driver).getAttribute("style");
		assertEquals(inner, oaindex.okstyle);
		oaindex.getjingdianstylemenu(driver).click();
		Thread.sleep(6000);
		inner = oaindex.getjingdianstyleok(driver).getAttribute("style");
		assertEquals(inner, oaindex.okstyle);
		
	}
	
	@DataProvider(name="link")
	public Object[][] links(){
		return new Object[][]{
			{"帮助","http://www.rockoa.com/help.html"},
		};
	}
	
	@Test(dataProvider="link", enabled=true)
	public void testlink(String linkText, String link) {
		driver.switchTo().window(handle);
		String url = oalogin.openLink(driver, linkText, handle);
		assertEquals(link,url);
	}
	
	@Test(enabled=true)
	public void testrefresh() {
		oaindex.getlink(driver, "刷新").click();
	}
	
	@Test(enabled=true)
	public void testremind() {
		oaindex.getpartiallink(driver, "提醒").click();
		String text = oaindex.getcentertable2(driver).getText();
		assertEquals(text, "提醒信息");
	}
	
	@Test(enabled=true)
	public void testmobile() {
		oaindex.getmobilebtn(driver).click();
		assertEquals(oaindex.getmobiletitle(driver).getText(),"登录手机版");
		oaindex.getmobilecancel(driver).click();
	}

	@Test(enabled=true)
	public void testopenchat() {
		String url = oaindex.openchat(driver, handle);
		assertTrue(url.contains("http://localhost/oa/webreim/client/index.html"));
	}
	
	@Test(enabled=true)
	public void testreload() {
		oaindex.getreloadbtn(driver).click();
	}
	
	@Test(enabled=true)
	public void testmax() throws InterruptedException {
		oaindex.getmaxbtn(driver).click();
		Thread.sleep(3000);
		assertTrue(!oaindex.getlink(driver, "退出").isDisplayed());
		oaindex.getmaxbtn(driver).click();
		Thread.sleep(3000);
		assertTrue(oaindex.getlink(driver, "退出").isDisplayed());
	}

	@Test
	public void testleftmenu() throws InterruptedException {
		leftmenu(doc, "person", false, "maninode", "node");
	    Thread.sleep(6000);
	    leftmenu(doc, "common", true, "maincommonnode", "commonnode");
	    Thread.sleep(6000);
	    leftmenu(doc, "system", true, "mainsystemnode", "systemnode");
        Thread.sleep(6000);
        leftmenu(doc, "human", true, "mainhumannode", "humannode");
	    Thread.sleep(6000);
        leftmenu(doc, "admin", true, "mainadminnode", "adminnode");
        Thread.sleep(6000);
	    leftmenu(doc, "task", true, "maintasknode", "tasknode");
	}
	
	public void leftmenu(Document doc, String topnode, Boolean flag, String mainnode, String node) throws InterruptedException {
		NodeList topnodes = doc.getElementsByTagName(topnode);
		String text2 = topnodes.item(0).getAttributes().getNamedItem("id").getNodeValue();
		String text3 = topnodes.item(0).getAttributes().getNamedItem("text").getNodeValue();
		WebElement topspan = oaindex.getelementbyid(driver, text2);
		if(flag) {
			topspan.click();
		}
		Thread.sleep(3000);
		String text1 = topspan.getText();
		assertEquals(text1, text3);
		oaindex.getexpandimg(driver).click();
		
		NodeList mainnodes = doc.getElementsByTagName(mainnode);
		for(int i = 0; i < mainnodes.getLength(); i++){
			text2 = mainnodes.item(i).getAttributes().getNamedItem("id").getNodeValue();
			text3 = mainnodes.item(i).getAttributes().getNamedItem("text").getNodeValue();
			WebElement span1 = oaindex.getelementbyxpath(driver, text2);
			text1 = span1.getText();
			assertTrue(text1.contains(text3));	
		}
		
		NodeList nodes = doc.getElementsByTagName(node);
		for(int i = 0; i < nodes.getLength(); i++){
			text2 = nodes.item(i).getAttributes().getNamedItem("id").getNodeValue();
			text3 = nodes.item(i).getAttributes().getNamedItem("text").getNodeValue();
			WebElement span1 = oaindex.getelementbyxpath(driver, text2);
			span1.click();
			text1 = span1.getText();
			assertTrue(text1.contains(text3));
			closecentertable();
		}
	}
	private void openonelefmenu(String topnode, Boolean flag, String node, int n) throws InterruptedException {
		NodeList topnodes = doc.getElementsByTagName(topnode);
		String text1 = topnodes.item(0).getAttributes().getNamedItem("id").getNodeValue();
		WebElement topspan = oaindex.getelementbyid(driver, text1);
		if(flag) {
			topspan.click();
		}
		Thread.sleep(3000);
		oaindex.getexpandimg(driver).click();
		NodeList nodes = doc.getElementsByTagName(node);
		String text2 = nodes.item(n).getAttributes().getNamedItem("id").getNodeValue();
		WebElement span1 = oaindex.getelementbyxpath(driver, text2);
		span1.click();
	}
	
	private void closecentertable() {
		WebElement centertable = oaindex.getcentertable2(driver);
		Actions action = new Actions(driver);
		action.contextClick(centertable).perform();
		oaindex.getpartiallink(driver, "关闭").click();
	}
	
	@Test(enabled=true)
	public void teschangepassword() throws InterruptedException {
		oaindex.getlink(driver, "退出").click();
		Thread.sleep(2000);
		oalogin.oa_login(driver, "mzg", "123456a");
		openonelefmenu("person",false,"node", 0);
		oaindex.getoldpassword(driver).clear();
		oaindex.getoldpassword(driver).sendKeys("123456a");
		oaindex.getpostpassword(driver).clear();
		oaindex.getpostpassword(driver).sendKeys("654321a");
		oaindex.getpostpasswordconfirm(driver).clear();
		oaindex.getpostpasswordconfirm(driver).sendKeys("654321a");
		//oaindex.getlink(driver, "修改").click();
		oaindex.getchangepassbtn(driver).click();;
		Thread.sleep(2000);
		closecentertable();
		Thread.sleep(3000);
		oaindex.getlink(driver, "退出").click();
		Thread.sleep(2000);
		oalogin.oa_login(driver, "mzg", "654321a");
		openonelefmenu("person",false,"node", 0);
		Thread.sleep(3000);
		oaindex.getoldpassword(driver).clear();
		oaindex.getoldpassword(driver).sendKeys("654321a");
		oaindex.getpostpassword(driver).clear();
		oaindex.getpostpassword(driver).sendKeys("123456a");
		oaindex.getpostpasswordconfirm(driver).clear();
		oaindex.getpostpasswordconfirm(driver).sendKeys("123456a");
		//oaindex.getlink(driver, "修改").click();
		oaindex.getchangepassbtn(driver).click();;
		Thread.sleep(2000);
		closecentertable();
		oaindex.getlink(driver, "退出").click();
		Thread.sleep(2000);
		oalogin.oa_login(driver, "admin", "123456");
	}
	
	@DataProvider(name="passwords")
	public Object[][] passwordswrong(){
		return new Object[][]{
			{"123456","1","654321a","新密码不能少于4个字符"},
			{"123456","654321","12","新密码必须使用字母+数字"},
			{"123456","654321a","12","确认密码不一致"},
			{"1232445","654321a","654321a","旧密码不正确"},
		};
	}
	
	@Test(dataProvider="passwords")
	public void testoaloginwithwrong(String oldpassword, String passwordpost, String passwordpostconfirm, String wrongstr) throws InterruptedException {
		openonelefmenu("person",false,"node", 0);
		oaindex.getoldpassword(driver).clear();
		oaindex.getoldpassword(driver).sendKeys(oldpassword);
		oaindex.getpostpassword(driver).clear();
		oaindex.getpostpassword(driver).sendKeys(passwordpost);
		oaindex.getpostpasswordconfirm(driver).clear();
		oaindex.getpostpasswordconfirm(driver).sendKeys(passwordpostconfirm);
		oaindex.getchangepassbtn(driver).click();
		Thread.sleep(2000);
		String wstr = oaindex.getchangepasswrongstr(driver).getText();
		assertEquals(wstr,wrongstr);
	}
	
	@Test()
	public void testaddkaoqinitem() throws InterruptedException {
		openonelefmenu("person",false,"node", 5);
		oaindex.getadditembtn(driver).click();
		oaindex.getkaoqinmenu(driver).click();
		Thread.sleep(2000);
		String text = oaindex.getkaoqinitemwind(driver).getText();
		assertEquals(text, "在线考勤");
		oaindex.getsaveitembtn(driver).click();
		Thread.sleep(2000);
		oaindex.getkaoqinitemwindclose(driver).click();
		Thread.sleep(2000);
		oaindex.getsaveitembtn(driver).click();
	}
	

}
